/*
 * Copyright (c) 2023 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import {HiLog} from './HiLog'
import data_preferences from '@ohos.data.preferences';

const TAG = "storage"

export class Storage{
    preferences:data_preferences.Preferences
    readonly TOKEN_KEY = "tokenKey"
    readonly HISTORY_KEY = "historyKey"
    constructor(){

    }

    private async getPreferences(){
        if (!this.preferences) {
            let preferences = await data_preferences.getPreferences(globalThis.context, 'music')
            this.preferences = preferences
        }
        return this.preferences
    }

    public async saveToken(token:string){
        HiLog.info(TAG,"start saveToken")
        let preferences = await this.getPreferences()
        let result = await preferences.put(this.TOKEN_KEY, token);
        // 将当前Preferences实例的数据异步存储到首选项持久化文件中
        preferences.flush((err)=>{
            if (err) {
                HiLog.error(TAG,'saveToken flush fail')
                return
            }
            HiLog.info(TAG,'saveToken flush success')
        })
        return result
    }

    public async clearToken(){
        let preferences = await this.getPreferences()
        let result = preferences.put(this.TOKEN_KEY, "")
        preferences.flush((err)=>{
            if (err) {
                HiLog.error(TAG,'clearToken flush fail')
                return
            }
            HiLog.info(TAG,'saveToken flush success')
        })
        return result;
    }

    public async getToken(){
        let preferences = await this.getPreferences()
        let token = await preferences.get(this.TOKEN_KEY,"");
        return token;
    }

    public async addSearchHistory(history:string){
        HiLog.info(TAG,"start addSearchHistory")
        let preferences = await this.getPreferences()
        let historyList = await this.getSearchHistoryList()
        historyList.push(history)
        let result = await preferences.put(this.HISTORY_KEY, historyList.toString());
        // 将当前Preferences实例的数据异步存储到首选项持久化文件中
        preferences.flush((err)=>{
            if (err) {
                HiLog.error(TAG,'addSearchHistory flush fail')
                return
            }
            HiLog.info(TAG,'addSearchHistory flush success')
        })
        return result
    }

    public async getSearchHistoryList(){
        let preferences = await this.getPreferences()
        let history = await preferences.get(this.HISTORY_KEY,"");
        if (history == "") {
            return []
        }else{
            let historyList = history.toString().split(',')
            return historyList;
        }
    }

    public async clearSearchHistoryList(){
        let preferences = await this.getPreferences()
        let result = preferences.put(this.HISTORY_KEY, "")
        preferences.flush((err)=>{
            if (err) {
                HiLog.error(TAG,'clearSearchHistoryList flush fail')
                return
            }
            HiLog.info(TAG,'clearSearchHistoryList flush success')
        })
        return result;
    }
}